------------------
function screen.drawSoftRect(screensr, xsra, ysra, xsrb, ysrb, couleursr)
   if (xsrb-xsra) > (ysrb-ysra) then
      rayonsr = math.floor((xsrb-xsra)*10/100)
   else
      rayonsr = math.floor((ysrb-ysra)*10/100)
   end
   a=0
   screen.drawFillRect(screensr, xsra+rayonsr, ysra, xsrb-rayonsr, ysrb, couleursr)
   screen.drawFillRect(screensr, xsra, ysra+rayonsr, xsrb, ysrb-rayonsr, couleursr)
   while a < (math.rad(360/4)) do
      screen.drawFillRect(screensr, xsrb-rayonsr, ysra+rayonsr, (math.cos(a)*rayonsr) + xsrb-rayonsr, -(math.sin(a)*rayonsr) + ysra+rayonsr, couleursr)
      screen.drawFillRect(screensr, xsrb-rayonsr, ysrb-rayonsr, (math.cos(a)*rayonsr) + xsrb-rayonsr, (math.sin(a)*rayonsr) + ysrb-rayonsr, couleursr)
      screen.drawFillRect(screensr, xsra+rayonsr, ysra+rayonsr, -(math.cos(a)*rayonsr) + xsra+rayonsr, -(math.sin(a)*rayonsr) + ysra+rayonsr, couleursr)
      screen.drawFillRect(screensr, xsra+rayonsr, ysrb-rayonsr, -(math.cos(a)*rayonsr) + xsra+rayonsr, (math.sin(a)*rayonsr) + ysrb-rayonsr, couleursr)
      a = a + 0.01
   end
end
------------------
dofile ("libColor.lua")
dofile ("StylusBox.lua")

Button = {}

bxx, bxy = 0, 0

function Button.init (sc, X, Y, Long, Haut, texte, but)
  
  local bu = tostring (but)
  
  c_buthaut1 = Color.mod (242, 242, 242)
  c_butbas1 = Color.mod (207, 207, 207)
  c_buthaut2 = Color.mod (229, 244, 252)
  c_butbas2 = Color.mod (104, 179, 219)
  c_butclkd = Color.mod (44, 98, 139)
  
  _G[bu] = {}
  
  _G[bu].scr = sc
  
  _G[bu].x = X
  _G[bu].y = Y
  
  _G[bu].x2 = X + Long
  _G[bu].y2 = Y + Haut
  
  _G[bu].txt = texte
  _G[bu].txtx = (_G[bu].x+Long/2)-((string.len (_G[bu].txt)*6)/2)
  _G[bu].txty = (_G[bu].y+Haut/2)-(9/2)
  _G[bu].c1 = c_buthaut1
  _G[bu].c2 = c_butbas1
  _G[bu].c3 = c_noir
  
  _G[bu].box = newBox (_G[bu].x, _G[bu].y, _G[bu].x2, _G[bu].y2)

end

function Button.draw (but)
  
  local bu = tostring (but)
  
  if Stylus.held then
    bxx = Stylus.X
    bxy = Stylus.Y
  end
  
  screen.drawSoftRect (_G[bu].scr, _G[bu].x, _G[bu].y, _G[bu].x2, _G[bu].y2, _G[bu].c3)
  screen.drawGradientRect (_G[bu].scr, _G[bu].x+1, _G[bu].y+1, _G[bu].x2-1, _G[bu].y2-1, _G[bu].c1, _G[bu].c1, _G[bu].c2, _G[bu].c2)
  screen.print (_G[bu].scr, _G[bu].txtx, _G[bu].txty, _G[bu].txt, c_noir)
    
  if Stylus.heldinBox (_G[bu].box, bxx, bxy) then
    _G[bu].c1 = c_buthaut2
    _G[bu].c2 = c_butbas2
    _G[bu].c3 = c_butclkd
  else 
    _G[bu].c1 = c_buthaut1
    _G[bu].c2 = c_butbas1
    _G[bu].c3 = c_noir
  end
end

function Button.clicked (but)
  local bu = tostring (but)
  
  local clicked = false
  
  if Stylus.newPressinBox (_G[bu].box, bxx, bxy) then
    clicked = true
  end
  
  return clicked
end

function Button.clear (but)
  local bu = tostring (but)
  
  _G[bu] = nil
  bxx = nil
  bxy = nil
end

